import 'package:flutter/material.dart';

class AppLocalizations {
  final Locale locale;

  AppLocalizations(this.locale);

  static AppLocalizations of(BuildContext context) {
    return Localizations.of<AppLocalizations>(context, AppLocalizations)!;
  }

  static const LocalizationsDelegate<AppLocalizations> delegate =
      _AppLocalizationsDelegate();

  static final Map<String, Map<String, String>> _localizedValues = {
    'en': {
      // Navigation
      'nav_dashboard': 'Dashboard',
      'nav_servers': 'Servers',
      'nav_subscriptions': 'Subscriptions',
      'nav_settings': 'Settings',
      
      // Connection
      'connection_connected': 'Connected',
      'connection_disconnected': 'Disconnected',
      'connection_connecting': 'Connecting...',
      'connection_disconnecting': 'Disconnecting...',
      'btn_connect': 'Connect',
      'btn_disconnect': 'Disconnect',
      'connection_duration': 'Duration',
      
      // Traffic
      'traffic_upload': 'Upload',
      'traffic_download': 'Download',
      'traffic_total': 'Total',
      'traffic_speed': 'Speed',
      'traffic_statistics': 'Traffic Statistics',
      
      // Server
      'server_add': 'Add Server',
      'server_edit': 'Edit Server',
      'server_delete': 'Delete Server',
      'server_name': 'Server Name',
      'server_address': 'Address',
      'server_port': 'Port',
      'server_protocol': 'Protocol',
      'server_security': 'Security',
      'server_network': 'Network',
      'server_recent': 'Recent Servers',
      'server_all': 'All Servers',
      'server_favorites': 'Favorites',
      'server_groups': 'Groups',
      'server_search': 'Search servers...',
      'server_test_ping': 'Test Ping',
      'server_test_all': 'Test All',
      'server_share': 'Share',
      'server_copy': 'Copy',
      
      // Subscription
      'subscription_add': 'Add Subscription',
      'subscription_edit': 'Edit Subscription',
      'subscription_delete': 'Delete Subscription',
      'subscription_name': 'Subscription Name',
      'subscription_url': 'Subscription URL',
      'subscription_update': 'Update',
      'subscription_update_all': 'Update All',
      'subscription_last_update': 'Last Updated',
      'subscription_servers': 'servers',
      
      // Settings
      'settings_connection': 'Connection',
      'settings_subscription': 'Subscription',
      'settings_core': 'Core',
      'settings_appearance': 'Appearance',
      'settings_about': 'About',
      'settings_auto_connect': 'Auto Connect',
      'settings_auto_connect_desc': 'Connect automatically on app start',
      'settings_system_proxy': 'System Proxy',
      'settings_system_proxy_desc': 'Configure system proxy when connected',
      'settings_auto_update': 'Auto Update',
      'settings_auto_update_desc': 'Update subscriptions automatically',
      'settings_update_interval': 'Update Interval',
      'settings_mux': 'Mux',
      'settings_mux_desc': 'Enable connection multiplexing',
      'settings_allow_insecure': 'Allow Insecure',
      'settings_allow_insecure_desc': 'Allow insecure TLS connections',
      'settings_theme': 'Theme',
      'settings_language': 'Language',
      'settings_version': 'Version',
      'settings_check_update': 'Check for Updates',
      
      // Theme
      'theme_light': 'Light',
      'theme_dark': 'Dark',
      'theme_system': 'System',
      
      // Actions
      'action_add': 'Add',
      'action_edit': 'Edit',
      'action_delete': 'Delete',
      'action_cancel': 'Cancel',
      'action_save': 'Save',
      'action_confirm': 'Confirm',
      'action_refresh': 'Refresh',
      'action_clear': 'Clear',
      'action_export': 'Export',
      'action_import': 'Import',
      
      // Messages
      'msg_connect_success': 'Connected successfully',
      'msg_connect_failed': 'Connection failed',
      'msg_disconnect_success': 'Disconnected',
      'msg_select_server': 'Please select a server first',
      'msg_testing_servers': 'Testing all servers...',
      'msg_delete_confirm': 'Are you sure you want to delete',
      'msg_no_servers': 'No servers available',
      'msg_no_subscriptions': 'No subscriptions',
      'msg_updating': 'Updating...',
      'msg_updated': 'Updated successfully',
      
      // Import Methods
      'import_manual': 'Manual Configuration',
      'import_qr': 'Scan QR Code',
      'import_clipboard': 'Import from Clipboard',
      'import_file': 'Import from File',
      
      // Proxy Modes
      'proxy_mode': 'Proxy Mode',
      'proxy_mode_global': 'Global',
      'proxy_mode_pac': 'PAC',
      'proxy_mode_direct': 'Direct',
      'proxy_mode_manual': 'Manual',
      
      // Quick Actions
      'quick_actions': 'Quick Actions',
      'quick_mode_switch': 'Mode Switch',
      'quick_test_ping': 'Test Ping',
      'quick_share_config': 'Share Config',
      'quick_copy_link': 'Copy Link',
      
      // Stats & Info
      'stats_today': 'Today',
      'stats_this_week': 'This Week',
      'stats_this_month': 'This Month',
      'stats_total_traffic': 'Total Traffic',
      'stats_avg_speed': 'Avg Speed',
      'stats_peak_speed': 'Peak Speed',
      'stats_sessions': 'Sessions',
      'stats_uptime': 'Uptime',
      'info_ping': 'Ping',
      'info_server_load': 'Server Load',
      'info_location': 'Location',
      
      // Notifications
      'notif_connected': 'VPN Connected',
      'notif_disconnected': 'VPN Disconnected',
      'notif_traffic_warning': 'Traffic Warning',
      'notif_traffic_limit': 'Approaching traffic limit',
      
      // Features
      'feature_speed_test': 'Speed Test',
      'feature_traffic_monitor': 'Traffic Monitor',
      'feature_connection_log': 'Connection Log',
      'feature_routing_rules': 'Routing Rules',
      'feature_dns_settings': 'DNS Settings',
      'feature_backup_restore': 'Backup & Restore',
      
      // Location
      'location_loading': 'Detecting location...',
      'location_current': 'Current Location',
      'location_ip_address': 'IP Address',
      'location_country': 'Country',
      'location_region': 'Region',
      'location_city': 'City',
      'location_isp': 'ISP',
      'location_unknown': 'Unknown Location',
      'location_original': 'Original IP',
      'location_proxy': 'Proxy IP',
      'location_original_location': 'Your Location',
      'location_proxy_location': 'Connected Location',
    },
    'zh': {
      // Navigation
      'nav_dashboard': '主页',
      'nav_servers': '服务器',
      'nav_subscriptions': '订阅',
      'nav_settings': '设置',
      
      // Connection
      'connection_connected': '已连接',
      'connection_disconnected': '未连接',
      'connection_connecting': '连接中...',
      'connection_disconnecting': '断开中...',
      'btn_connect': '连接',
      'btn_disconnect': '断开',
      'connection_duration': '连接时长',
      
      // Traffic
      'traffic_upload': '上传',
      'traffic_download': '下载',
      'traffic_total': '总计',
      'traffic_speed': '速度',
      'traffic_statistics': '流量统计',
      
      // Server
      'server_add': '添加服务器',
      'server_edit': '编辑服务器',
      'server_delete': '删除服务器',
      'server_name': '服务器名称',
      'server_address': '地址',
      'server_port': '端口',
      'server_protocol': '协议',
      'server_security': '加密',
      'server_network': '传输协议',
      'server_recent': '最近使用',
      'server_all': '全部服务器',
      'server_favorites': '收藏',
      'server_groups': '分组',
      'server_search': '搜索服务器...',
      'server_test_ping': '测速',
      'server_test_all': '测试全部',
      'server_share': '分享',
      'server_copy': '复制',
      
      // Subscription
      'subscription_add': '添加订阅',
      'subscription_edit': '编辑订阅',
      'subscription_delete': '删除订阅',
      'subscription_name': '订阅名称',
      'subscription_url': '订阅链接',
      'subscription_update': '更新',
      'subscription_update_all': '更新全部',
      'subscription_last_update': '最后更新',
      'subscription_servers': '个服务器',
      
      // Settings
      'settings_connection': '连接设置',
      'settings_subscription': '订阅设置',
      'settings_core': '核心设置',
      'settings_appearance': '外观设置',
      'settings_about': '关于',
      'settings_auto_connect': '自动连接',
      'settings_auto_connect_desc': '应用启动时自动连接',
      'settings_system_proxy': '系统代理',
      'settings_system_proxy_desc': '连接时配置系统代理',
      'settings_auto_update': '自动更新',
      'settings_auto_update_desc': '自动更新订阅',
      'settings_update_interval': '更新间隔',
      'settings_mux': '多路复用',
      'settings_mux_desc': '启用连接多路复用',
      'settings_allow_insecure': '允许不安全连接',
      'settings_allow_insecure_desc': '允许不安全的 TLS 连接',
      'settings_theme': '主题',
      'settings_language': '语言',
      'settings_version': '版本',
      'settings_check_update': '检查更新',
      
      // Theme
      'theme_light': '浅色',
      'theme_dark': '深色',
      'theme_system': '跟随系统',
      
      // Actions
      'action_add': '添加',
      'action_edit': '编辑',
      'action_delete': '删除',
      'action_cancel': '取消',
      'action_save': '保存',
      'action_confirm': '确认',
      'action_refresh': '刷新',
      'action_clear': '清除',
      'action_export': '导出',
      'action_import': '导入',
      
      // Messages
      'msg_connect_success': '连接成功',
      'msg_connect_failed': '连接失败',
      'msg_disconnect_success': '已断开连接',
      'msg_select_server': '请先选择服务器',
      'msg_testing_servers': '正在测试所有服务器...',
      'msg_delete_confirm': '确定要删除',
      'msg_no_servers': '暂无服务器',
      'msg_no_subscriptions': '暂无订阅',
      'msg_updating': '更新中...',
      'msg_updated': '更新成功',
      
      // Import Methods
      'import_manual': '手动配置',
      'import_qr': '扫描二维码',
      'import_clipboard': '从剪贴板导入',
      'import_file': '从文件导入',
      
      // Proxy Modes
      'proxy_mode': '代理模式',
      'proxy_mode_global': '全局',
      'proxy_mode_pac': 'PAC',
      'proxy_mode_direct': '直连',
      'proxy_mode_manual': '手动',
      
      // Quick Actions
      'quick_actions': '快捷操作',
      'quick_mode_switch': '模式切换',
      'quick_test_ping': '延迟测试',
      'quick_share_config': '分享配置',
      'quick_copy_link': '复制链接',
      
      // Stats & Info
      'stats_today': '今日',
      'stats_this_week': '本周',
      'stats_this_month': '本月',
      'stats_total_traffic': '总流量',
      'stats_avg_speed': '平均速度',
      'stats_peak_speed': '峰值速度',
      'stats_sessions': '会话数',
      'stats_uptime': '运行时间',
      'info_ping': '延迟',
      'info_server_load': '服务器负载',
      'info_location': '位置',
      
      // Notifications
      'notif_connected': 'VPN 已连接',
      'notif_disconnected': 'VPN 已断开',
      'notif_traffic_warning': '流量警告',
      'notif_traffic_limit': '接近流量限制',
      
      // Features
      'feature_speed_test': '网速测试',
      'feature_traffic_monitor': '流量监控',
      'feature_connection_log': '连接日志',
      'feature_routing_rules': '路由规则',
      'feature_dns_settings': 'DNS 设置',
      'feature_backup_restore': '备份与恢复',
      
      // Location
      'location_loading': '正在检测位置...',
      'location_current': '当前位置',
      'location_ip_address': 'IP 地址',
      'location_country': '国家',
      'location_region': '地区',
      'location_city': '城市',
      'location_isp': '运营商',
      'location_unknown': '未知位置',
      'location_original': '本机 IP',
      'location_proxy': '代理 IP',
      'location_original_location': '您的位置',
      'location_proxy_location': '连接位置',
    },
  };

  String translate(String key) {
    return _localizedValues[locale.languageCode]?[key] ?? key;
  }

  // Getters for easy access
  String get navDashboard => translate('nav_dashboard');
  String get navServers => translate('nav_servers');
  String get navSubscriptions => translate('nav_subscriptions');
  String get navSettings => translate('nav_settings');
  
  String get connectionConnected => translate('connection_connected');
  String get connectionDisconnected => translate('connection_disconnected');
  String get connectionConnecting => translate('connection_connecting');
  String get connectionDisconnecting => translate('connection_disconnecting');
  String get btnConnect => translate('btn_connect');
  String get btnDisconnect => translate('btn_disconnect');
  String get connectionDuration => translate('connection_duration');
  
  String get trafficUpload => translate('traffic_upload');
  String get trafficDownload => translate('traffic_download');
  String get trafficTotal => translate('traffic_total');
  String get trafficSpeed => translate('traffic_speed');
  String get trafficStatistics => translate('traffic_statistics');
  
  String get locationOriginal => translate('location_original');
  String get locationProxy => translate('location_proxy');
  String get locationOriginalLocation => translate('location_original_location');
  String get locationProxyLocation => translate('location_proxy_location');
  String get locationIpAddress => translate('location_ip_address');
  String get locationLoading => translate('location_loading');
  String get locationUnknown => translate('location_unknown');
}

class _AppLocalizationsDelegate
    extends LocalizationsDelegate<AppLocalizations> {
  const _AppLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) {
    return ['en', 'zh'].contains(locale.languageCode);
  }

  @override
  Future<AppLocalizations> load(Locale locale) async {
    return AppLocalizations(locale);
  }

  @override
  bool shouldReload(_AppLocalizationsDelegate old) => false;
}
